package com.mhy.mylog;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @author mahongyin 2020-04-17 9:14 @CopyRight mhy.work@qq.com
 * @description: 单线程使用StringBuilder快
 */
public class MyLog {
        private static String className;
        private static String methodName;
        private static int lineNumber;
        private static final int JSON_INDENT = 4;


        public static void d(String message) {
            getMethodNames(new Throwable().getStackTrace(),1);
            Log.d(className, createLog(message));
        }

        public static void d2(String message) {
            getMethodNames(new Throwable().getStackTrace(),2);
            Log.d(className, createLog(message));
        }
        public static void w(String message) {
            getMethodNames(new Throwable().getStackTrace(),1);
            Log.w(className, createLog(message));
        }
        public static void d(Object o) {
            String message=null;
            if (o==null){
                message="message is null";
            }else{
                message=o.toString();
            }
            getMethodNames(new Throwable().getStackTrace(),1);
            Log.d(className, createLog(message));
        }
        private static String createLog(String log) {
            StringBuilder buffer = new StringBuilder();
            buffer.append("[");
            buffer.append("(");
            buffer.append(className);
            buffer.append(":");
            buffer.append(lineNumber);
            buffer.append(")");
            buffer.append("#");
            buffer.append(methodName);
            buffer.append("]");
            buffer.append(printIfJson(log));
            return buffer.toString();
        }
        public static String printIfJson(String msg) {

            String message;
            StringBuilder stringBuffer=new StringBuilder();
            try {
                int index=-1;
                if(msg.contains("{")){
                    index=msg.indexOf("{");
                }else if (msg.contains("[")){
                    index=msg.indexOf("{");
                }

                if (index!=-1){
                    stringBuffer.append(msg.substring(0,index));
                    msg=msg.substring(index);
                }

                if (msg.startsWith("{")) {
                    JSONObject jsonObject = new JSONObject(msg);
                    message = jsonObject.toString(JSON_INDENT);
                } else if (msg.startsWith("[")) {
                    JSONArray jsonArray = new JSONArray(msg);
                    message = jsonArray.toString(JSON_INDENT);
                } else {
                    message = msg;
                }
            } catch (Exception e) {
                message = msg;
            }
            stringBuffer.append(message);
            return stringBuffer.toString();
        }
        private static void getMethodNames(StackTraceElement[] sElements, int index) {
            className = sElements[index].getFileName();
            methodName = sElements[index].getMethodName();
            lineNumber = sElements[index].getLineNumber();
        }


}
